<template>
  <el-dialog @close="tjyfkpzCancel('tjyfkpzRuleForm')" :close-on-click-modal="false" title="提交预付款凭证"
    :visible.sync="tjyfkpzVisible" width="600px">
    <div class="scroll-view">
      <el-form :model="tjyfkpzRuleForm" :rules="tjyfkpzRules" ref="tjyfkpzRuleForm" label-position="top">
        <el-form-item label="预付款金额" prop="amount_collected">
          <el-input class="input-box" v-model="tjyfkpzRuleForm.amount_collected" v-input-limit="2" clearable></el-input>
        </el-form-item>
        <el-form-item label="收款账户" prop="receiving_account_id">
          <el-select filterable class="input-box" v-model="tjyfkpzRuleForm.receiving_account_id" placeholder="请选择"
            clearable>
            <el-option v-for="item in companyList" :key="item.id" :label="item.account_name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预付款凭证 (.jpg,.png,.gif)" prop="payment_voucher">
          <el-input style="position: absolute; z-index: 0; top: -999999px"
            v-model="tjyfkpzRuleForm.payment_voucher.length"></el-input>
          <el-upload :on-change="(file, fileList) => {
              tjyfkpzChangeFile(file, fileList, 'payment_voucher');
            }
            " :on-remove="(file, fileList) => {
                tjyfkpzFileRemove(file, fileList, 'payment_voucher');
              }
              " :auto-upload="false" :file-list="tjyfkpzRuleForm.payment_voucher" action="#" accept="image/*"><el-button
              size="small" icon="el-icon-plus">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :rows="4" class="input-box" v-model="tjyfkpzRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button type="primary" @click="tjyfkpzSubmit('tjyfkpzRuleForm')" :disabled="diaLoading"
        :loading="diaLoading">提交</el-button>
      <el-button @click="tjyfkpzCancel('tjyfkpzRuleForm')">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
    signing_company_data: {
      required: false,
    },
  },
  data() {
    var paymentVoucher = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传凭证"));
      } else {
        callback();
      }
    };
    return {
      tjyfkpzVisible: false,
      tjyfkpzRuleForm: {
        amount_collected: "",
        receiving_account_id: "",
        remark: "",
        payment_voucher: [],
      },
      tjyfkpzRules: {
        amount_collected: [
          { required: true, message: "请输入预付款金额", trigger: "change" },
        ],
        receiving_account_id: [
          { required: true, message: "请选择收款账户", trigger: "change" },
        ],
        remark: "",
        payment_voucher: {
          required: true,
          validator: paymentVoucher,
          trigger: "change",
        },
      },
      diaLoading: false,
      companyList: [],
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      if (this.addDialogVisible) {
        this.tjyfkpzRuleForm.receiving_account_id = this.signing_company_data.signing_company_id;
        this.getCompanyList(1);
      }
      this.tjyfkpzVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() { },
  methods: {
    // 收款账户列表
    getCompanyList(account_type) {
      companyApi
        .getCompanyList({
          page: 1,
          num: 9999,
          keyword: "",
          account_type,
        })
        .then((res) => {
          console.log(res, account_type == 1 ? "公司列表" : "收款账户");
          if (res.code == 200) {
            this.companyList = res.data.list;
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch((err) => {
          this.$message.warning(err.message);
        });
    },
    // 提交预付款凭证
    tjyfkpzCancel(formName) {
      this.tjyfkpzRuleForm = {
        amount_collected: "",
        receiving_account_id: "",
        remark: "",
        payment_voucher: [],
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    tjyfkpzSubmit(formName) {
      let tjyfkpzRuleForm = this.tjyfkpzRuleForm;
      console.log(tjyfkpzRuleForm, "提交支付屏证参数");
      let companyList = this.companyList;
      let receiving_account_name = "";
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let index = companyList.findIndex((item) => {
            return item.id == tjyfkpzRuleForm.receiving_account_id;
          });
          receiving_account_name = companyList[index].account_name;
          this.diaLoading = true;
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              project_id: this.project_id,
              amount_collected: tjyfkpzRuleForm.amount_collected,
              receiving_account_id: tjyfkpzRuleForm.receiving_account_id,
              receiving_account_name,
              remark: tjyfkpzRuleForm.remark,
            })
          );
          tjyfkpzRuleForm.payment_voucher.forEach((item) => {
            formdata.append("payment_voucher[]", item.raw);
          });
          projectApi
            .submitAdvancePayment(formdata)
            .then((res) => {
              console.log(res, "提交支付屏证");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    tjyfkpzChangeFile(file, list, type) {
      let tjyfkpzRuleForm = this.tjyfkpzRuleForm;
      tjyfkpzRuleForm.payment_voucher = [file];
      this.tjyfkpzRuleForm = tjyfkpzRuleForm;
    },

    tjyfkpzFileRemove(file, list, type) {
      let tjyfkpzRuleForm = this.tjyfkpzRuleForm;
      tjyfkpzRuleForm.payment_voucher = list;
      this.tjyfkpzRuleForm = tjyfkpzRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>